/********************************************************************************
 * Copyright (c) 2008 - 2017 Profactor GmbH, TU Wien ACIN, fortiss GmbH
 *
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License 2.0 which is available at
 * http://www.eclipse.org/legal/epl-2.0.
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *  Gerhard Ebenhofer, Alois Zoitl, Ingo Hegny, Monika Wenger
 *    - initial API and implementation and/or initial documentation
 ********************************************************************************/
package org.eclipse.fordiac.ide.model.libraryElement.impl;

import org.eclipse.emf.common.notify.Notification;
import org.eclipse.emf.common.notify.NotificationChain;
import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.InternalEObject;
import org.eclipse.emf.ecore.impl.ENotificationImpl;
import org.eclipse.emf.ecore.util.EcoreUtil;
import org.eclipse.fordiac.ide.model.libraryElement.ECC;
import org.eclipse.fordiac.ide.model.libraryElement.ECState;
import org.eclipse.fordiac.ide.model.libraryElement.ECTransition;
import org.eclipse.fordiac.ide.model.libraryElement.Event;
import org.eclipse.fordiac.ide.model.libraryElement.LibraryElementPackage;

/**
 * <!-- begin-user-doc --> An implementation of the model object '<em><b>EC
 * Transition</b></em>'. <!-- end-user-doc -->
 * <p>
 * The following features are implemented:
 * </p>
 * <ul>
 * <li>{@link org.eclipse.fordiac.ide.model.libraryElement.impl.ECTransitionImpl#getComment
 * <em>Comment</em>}</li>
 * <li>{@link org.eclipse.fordiac.ide.model.libraryElement.impl.ECTransitionImpl#getConditionExpression
 * <em>Condition Expression</em>}</li>
 * <li>{@link org.eclipse.fordiac.ide.model.libraryElement.impl.ECTransitionImpl#getSource
 * <em>Source</em>}</li>
 * <li>{@link org.eclipse.fordiac.ide.model.libraryElement.impl.ECTransitionImpl#getDestination
 * <em>Destination</em>}</li>
 * <li>{@link org.eclipse.fordiac.ide.model.libraryElement.impl.ECTransitionImpl#getConditionEvent
 * <em>Condition Event</em>}</li>
 * <li>{@link org.eclipse.fordiac.ide.model.libraryElement.impl.ECTransitionImpl#getECC
 * <em>ECC</em>}</li>
 * </ul>
 *
 * @generated
 */
public class ECTransitionImpl extends PositionableElementImpl implements ECTransition {
	/**
	 * The default value of the '{@link #getComment() <em>Comment</em>}' attribute.
	 * <!-- begin-user-doc --> <!-- end-user-doc -->
	 * 
	 * @see #getComment()
	 * @generated
	 * @ordered
	 */
	protected static final String COMMENT_EDEFAULT = ""; //$NON-NLS-1$

	/**
	 * The cached value of the '{@link #getComment() <em>Comment</em>}' attribute.
	 * <!-- begin-user-doc --> <!-- end-user-doc -->
	 * 
	 * @see #getComment()
	 * @generated
	 * @ordered
	 */
	protected String comment = COMMENT_EDEFAULT;

	/**
	 * The default value of the '{@link #getConditionExpression() <em>Condition
	 * Expression</em>}' attribute. <!-- begin-user-doc --> <!-- end-user-doc -->
	 * 
	 * @see #getConditionExpression()
	 * @generated
	 * @ordered
	 */
	protected static final String CONDITION_EXPRESSION_EDEFAULT = "1"; //$NON-NLS-1$

	/**
	 * The cached value of the '{@link #getConditionExpression() <em>Condition
	 * Expression</em>}' attribute. <!-- begin-user-doc --> <!-- end-user-doc -->
	 * 
	 * @see #getConditionExpression()
	 * @generated
	 * @ordered
	 */
	protected String conditionExpression = CONDITION_EXPRESSION_EDEFAULT;

	/**
	 * The cached value of the '{@link #getSource() <em>Source</em>}' reference.
	 * <!-- begin-user-doc --> <!-- end-user-doc -->
	 * 
	 * @see #getSource()
	 * @generated
	 * @ordered
	 */
	protected ECState source;

	/**
	 * The cached value of the '{@link #getDestination() <em>Destination</em>}'
	 * reference. <!-- begin-user-doc --> <!-- end-user-doc -->
	 * 
	 * @see #getDestination()
	 * @generated
	 * @ordered
	 */
	protected ECState destination;

	/**
	 * The cached value of the '{@link #getConditionEvent() <em>Condition
	 * Event</em>}' reference. <!-- begin-user-doc --> <!-- end-user-doc -->
	 * 
	 * @see #getConditionEvent()
	 * @generated
	 * @ordered
	 */
	protected Event conditionEvent;

	/**
	 * <!-- begin-user-doc --> <!-- end-user-doc -->
	 * 
	 * @generated
	 */
	protected ECTransitionImpl() {
		super();
	}

	/**
	 * <!-- begin-user-doc --> <!-- end-user-doc -->
	 * 
	 * @generated
	 */
	@Override
	protected EClass eStaticClass() {
		return LibraryElementPackage.Literals.EC_TRANSITION;
	}

	/**
	 * <!-- begin-user-doc --> <!-- end-user-doc -->
	 * 
	 * @generated
	 */
	@Override
	public String getComment() {
		return comment;
	}

	/**
	 * <!-- begin-user-doc --> <!-- end-user-doc -->
	 * 
	 * @generated
	 */
	@Override
	public void setComment(String newComment) {
		String oldComment = comment;
		comment = newComment;
		if (eNotificationRequired())
			eNotify(new ENotificationImpl(this, Notification.SET, LibraryElementPackage.EC_TRANSITION__COMMENT,
					oldComment, comment));
	}

	/**
	 * <!-- begin-user-doc --> <!-- end-user-doc -->
	 * 
	 * @generated
	 */
	@Override
	public String getConditionExpression() {
		return conditionExpression;
	}

	/**
	 * <!-- begin-user-doc --> <!-- end-user-doc -->
	 * 
	 * @generated
	 */
	@Override
	public void setConditionExpression(String newConditionExpression) {
		String oldConditionExpression = conditionExpression;
		conditionExpression = newConditionExpression;
		if (eNotificationRequired())
			eNotify(new ENotificationImpl(this, Notification.SET,
					LibraryElementPackage.EC_TRANSITION__CONDITION_EXPRESSION, oldConditionExpression,
					conditionExpression));
	}

	/**
	 * <!-- begin-user-doc --> <!-- end-user-doc -->
	 * 
	 * @generated
	 */
	@Override
	public ECState getDestination() {
		if (destination != null && destination.eIsProxy()) {
			InternalEObject oldDestination = (InternalEObject) destination;
			destination = (ECState) eResolveProxy(oldDestination);
			if (destination != oldDestination) {
				if (eNotificationRequired())
					eNotify(new ENotificationImpl(this, Notification.RESOLVE,
							LibraryElementPackage.EC_TRANSITION__DESTINATION, oldDestination, destination));
			}
		}
		return destination;
	}

	/**
	 * <!-- begin-user-doc --> <!-- end-user-doc -->
	 * 
	 * @generated
	 */
	public ECState basicGetDestination() {
		return destination;
	}

	/**
	 * <!-- begin-user-doc --> <!-- end-user-doc -->
	 * 
	 * @generated
	 */
	public NotificationChain basicSetDestination(ECState newDestination, NotificationChain msgs) {
		ECState oldDestination = destination;
		destination = newDestination;
		if (eNotificationRequired()) {
			ENotificationImpl notification = new ENotificationImpl(this, Notification.SET,
					LibraryElementPackage.EC_TRANSITION__DESTINATION, oldDestination, newDestination);
			if (msgs == null)
				msgs = notification;
			else
				msgs.add(notification);
		}
		return msgs;
	}

	/**
	 * <!-- begin-user-doc --> <!-- end-user-doc -->
	 * 
	 * @generated
	 */
	@Override
	public void setDestination(ECState newDestination) {
		if (newDestination != destination) {
			NotificationChain msgs = null;
			if (destination != null)
				msgs = ((InternalEObject) destination).eInverseRemove(this,
						LibraryElementPackage.EC_STATE__IN_TRANSITIONS, ECState.class, msgs);
			if (newDestination != null)
				msgs = ((InternalEObject) newDestination).eInverseAdd(this,
						LibraryElementPackage.EC_STATE__IN_TRANSITIONS, ECState.class, msgs);
			msgs = basicSetDestination(newDestination, msgs);
			if (msgs != null)
				msgs.dispatch();
		} else if (eNotificationRequired())
			eNotify(new ENotificationImpl(this, Notification.SET, LibraryElementPackage.EC_TRANSITION__DESTINATION,
					newDestination, newDestination));
	}

	/**
	 * <!-- begin-user-doc --> <!-- end-user-doc -->
	 * 
	 * @generated
	 */
	@Override
	public Event getConditionEvent() {
		if (conditionEvent != null && conditionEvent.eIsProxy()) {
			InternalEObject oldConditionEvent = (InternalEObject) conditionEvent;
			conditionEvent = (Event) eResolveProxy(oldConditionEvent);
			if (conditionEvent != oldConditionEvent) {
				if (eNotificationRequired())
					eNotify(new ENotificationImpl(this, Notification.RESOLVE,
							LibraryElementPackage.EC_TRANSITION__CONDITION_EVENT, oldConditionEvent, conditionEvent));
			}
		}
		return conditionEvent;
	}

	/**
	 * <!-- begin-user-doc --> <!-- end-user-doc -->
	 * 
	 * @generated
	 */
	public Event basicGetConditionEvent() {
		return conditionEvent;
	}

	/**
	 * <!-- begin-user-doc --> <!-- end-user-doc -->
	 * 
	 * @generated
	 */
	@Override
	public void setConditionEvent(Event newConditionEvent) {
		Event oldConditionEvent = conditionEvent;
		conditionEvent = newConditionEvent;
		if (eNotificationRequired())
			eNotify(new ENotificationImpl(this, Notification.SET, LibraryElementPackage.EC_TRANSITION__CONDITION_EVENT,
					oldConditionEvent, conditionEvent));
	}

	/**
	 * <!-- begin-user-doc --> <!-- end-user-doc -->
	 * 
	 * @generated
	 */
	@Override
	public String getConditionText() {
		return org.eclipse.fordiac.ide.model.Annotations.getConditionText(this);
	}

	/**
	 * <!-- begin-user-doc --> <!-- end-user-doc -->
	 * 
	 * @generated
	 */
	@Override
	public int getPriority() {
		return getSource().getOutTransitions().indexOf(this) + 1;
	}

	/**
	 * <!-- begin-user-doc --> <!-- end-user-doc -->
	 * 
	 * @generated
	 */
	@Override
	public ECC getECC() {
		if (eContainerFeatureID() != LibraryElementPackage.EC_TRANSITION__ECC)
			return null;
		return (ECC) eContainer();
	}

	/**
	 * <!-- begin-user-doc --> <!-- end-user-doc -->
	 * 
	 * @generated
	 */
	public ECC basicGetECC() {
		if (eContainerFeatureID() != LibraryElementPackage.EC_TRANSITION__ECC)
			return null;
		return (ECC) eInternalContainer();
	}

	/**
	 * <!-- begin-user-doc --> <!-- end-user-doc -->
	 * 
	 * @generated
	 */
	public NotificationChain basicSetECC(ECC newECC, NotificationChain msgs) {
		msgs = eBasicSetContainer((InternalEObject) newECC, LibraryElementPackage.EC_TRANSITION__ECC, msgs);
		return msgs;
	}

	/**
	 * <!-- begin-user-doc --> <!-- end-user-doc -->
	 * 
	 * @generated
	 */
	@Override
	public void setECC(ECC newECC) {
		if (newECC != eInternalContainer()
				|| (eContainerFeatureID() != LibraryElementPackage.EC_TRANSITION__ECC && newECC != null)) {
			if (EcoreUtil.isAncestor(this, newECC))
				throw new IllegalArgumentException("Recursive containment not allowed for " + toString()); //$NON-NLS-1$
			NotificationChain msgs = null;
			if (eInternalContainer() != null)
				msgs = eBasicRemoveFromContainer(msgs);
			if (newECC != null)
				msgs = ((InternalEObject) newECC).eInverseAdd(this, LibraryElementPackage.ECC__EC_TRANSITION, ECC.class,
						msgs);
			msgs = basicSetECC(newECC, msgs);
			if (msgs != null)
				msgs.dispatch();
		} else if (eNotificationRequired())
			eNotify(new ENotificationImpl(this, Notification.SET, LibraryElementPackage.EC_TRANSITION__ECC, newECC,
					newECC));
	}

	/**
	 * <!-- begin-user-doc --> <!-- end-user-doc -->
	 * 
	 * @generated
	 */
	@Override
	public NotificationChain eInverseAdd(InternalEObject otherEnd, int featureID, NotificationChain msgs) {
		switch (featureID) {
		case LibraryElementPackage.EC_TRANSITION__SOURCE:
			if (source != null)
				msgs = ((InternalEObject) source).eInverseRemove(this, LibraryElementPackage.EC_STATE__OUT_TRANSITIONS,
						ECState.class, msgs);
			return basicSetSource((ECState) otherEnd, msgs);
		case LibraryElementPackage.EC_TRANSITION__DESTINATION:
			if (destination != null)
				msgs = ((InternalEObject) destination).eInverseRemove(this,
						LibraryElementPackage.EC_STATE__IN_TRANSITIONS, ECState.class, msgs);
			return basicSetDestination((ECState) otherEnd, msgs);
		case LibraryElementPackage.EC_TRANSITION__ECC:
			if (eInternalContainer() != null)
				msgs = eBasicRemoveFromContainer(msgs);
			return basicSetECC((ECC) otherEnd, msgs);
		default:
			return super.eInverseAdd(otherEnd, featureID, msgs);
		}
	}

	/**
	 * <!-- begin-user-doc --> <!-- end-user-doc -->
	 * 
	 * @generated
	 */
	@Override
	public NotificationChain eInverseRemove(InternalEObject otherEnd, int featureID, NotificationChain msgs) {
		switch (featureID) {
		case LibraryElementPackage.EC_TRANSITION__SOURCE:
			return basicSetSource(null, msgs);
		case LibraryElementPackage.EC_TRANSITION__DESTINATION:
			return basicSetDestination(null, msgs);
		case LibraryElementPackage.EC_TRANSITION__ECC:
			return basicSetECC(null, msgs);
		default:
			return super.eInverseRemove(otherEnd, featureID, msgs);
		}
	}

	/**
	 * <!-- begin-user-doc --> <!-- end-user-doc -->
	 * 
	 * @generated
	 */
	@Override
	public NotificationChain eBasicRemoveFromContainerFeature(NotificationChain msgs) {
		switch (eContainerFeatureID()) {
		case LibraryElementPackage.EC_TRANSITION__ECC:
			return eInternalContainer().eInverseRemove(this, LibraryElementPackage.ECC__EC_TRANSITION, ECC.class, msgs);
		default:
			return super.eBasicRemoveFromContainerFeature(msgs);
		}
	}

	/**
	 * <!-- begin-user-doc --> <!-- end-user-doc -->
	 * 
	 * @generated
	 */
	@Override
	public ECState getSource() {
		if (source != null && source.eIsProxy()) {
			InternalEObject oldSource = (InternalEObject) source;
			source = (ECState) eResolveProxy(oldSource);
			if (source != oldSource) {
				if (eNotificationRequired())
					eNotify(new ENotificationImpl(this, Notification.RESOLVE,
							LibraryElementPackage.EC_TRANSITION__SOURCE, oldSource, source));
			}
		}
		return source;
	}

	/**
	 * <!-- begin-user-doc --> <!-- end-user-doc -->
	 * 
	 * @generated
	 */
	public ECState basicGetSource() {
		return source;
	}

	/**
	 * <!-- begin-user-doc --> <!-- end-user-doc -->
	 * 
	 * @generated
	 */
	public NotificationChain basicSetSource(ECState newSource, NotificationChain msgs) {
		ECState oldSource = source;
		source = newSource;
		if (eNotificationRequired()) {
			ENotificationImpl notification = new ENotificationImpl(this, Notification.SET,
					LibraryElementPackage.EC_TRANSITION__SOURCE, oldSource, newSource);
			if (msgs == null)
				msgs = notification;
			else
				msgs.add(notification);
		}
		return msgs;
	}

	/**
	 * <!-- begin-user-doc --> <!-- end-user-doc -->
	 * 
	 * @generated
	 */
	@Override
	public void setSource(ECState newSource) {
		if (newSource != source) {
			NotificationChain msgs = null;
			if (source != null)
				msgs = ((InternalEObject) source).eInverseRemove(this, LibraryElementPackage.EC_STATE__OUT_TRANSITIONS,
						ECState.class, msgs);
			if (newSource != null)
				msgs = ((InternalEObject) newSource).eInverseAdd(this, LibraryElementPackage.EC_STATE__OUT_TRANSITIONS,
						ECState.class, msgs);
			msgs = basicSetSource(newSource, msgs);
			if (msgs != null)
				msgs.dispatch();
		} else if (eNotificationRequired())
			eNotify(new ENotificationImpl(this, Notification.SET, LibraryElementPackage.EC_TRANSITION__SOURCE,
					newSource, newSource));
	}

	/**
	 * <!-- begin-user-doc --> <!-- end-user-doc -->
	 * 
	 * @generated
	 */
	@Override
	public Object eGet(int featureID, boolean resolve, boolean coreType) {
		switch (featureID) {
		case LibraryElementPackage.EC_TRANSITION__COMMENT:
			return getComment();
		case LibraryElementPackage.EC_TRANSITION__CONDITION_EXPRESSION:
			return getConditionExpression();
		case LibraryElementPackage.EC_TRANSITION__SOURCE:
			if (resolve)
				return getSource();
			return basicGetSource();
		case LibraryElementPackage.EC_TRANSITION__DESTINATION:
			if (resolve)
				return getDestination();
			return basicGetDestination();
		case LibraryElementPackage.EC_TRANSITION__CONDITION_EVENT:
			if (resolve)
				return getConditionEvent();
			return basicGetConditionEvent();
		case LibraryElementPackage.EC_TRANSITION__ECC:
			if (resolve)
				return getECC();
			return basicGetECC();
		default:
			return super.eGet(featureID, resolve, coreType);
		}
	}

	/**
	 * <!-- begin-user-doc --> <!-- end-user-doc -->
	 * 
	 * @generated
	 */
	@Override
	public void eSet(int featureID, Object newValue) {
		switch (featureID) {
		case LibraryElementPackage.EC_TRANSITION__COMMENT:
			setComment((String) newValue);
			return;
		case LibraryElementPackage.EC_TRANSITION__CONDITION_EXPRESSION:
			setConditionExpression((String) newValue);
			return;
		case LibraryElementPackage.EC_TRANSITION__SOURCE:
			setSource((ECState) newValue);
			return;
		case LibraryElementPackage.EC_TRANSITION__DESTINATION:
			setDestination((ECState) newValue);
			return;
		case LibraryElementPackage.EC_TRANSITION__CONDITION_EVENT:
			setConditionEvent((Event) newValue);
			return;
		case LibraryElementPackage.EC_TRANSITION__ECC:
			setECC((ECC) newValue);
			return;
		default:
			super.eSet(featureID, newValue);
			return;
		}
	}

	/**
	 * <!-- begin-user-doc --> <!-- end-user-doc -->
	 * 
	 * @generated
	 */
	@Override
	public void eUnset(int featureID) {
		switch (featureID) {
		case LibraryElementPackage.EC_TRANSITION__COMMENT:
			setComment(COMMENT_EDEFAULT);
			return;
		case LibraryElementPackage.EC_TRANSITION__CONDITION_EXPRESSION:
			setConditionExpression(CONDITION_EXPRESSION_EDEFAULT);
			return;
		case LibraryElementPackage.EC_TRANSITION__SOURCE:
			setSource((ECState) null);
			return;
		case LibraryElementPackage.EC_TRANSITION__DESTINATION:
			setDestination((ECState) null);
			return;
		case LibraryElementPackage.EC_TRANSITION__CONDITION_EVENT:
			setConditionEvent((Event) null);
			return;
		case LibraryElementPackage.EC_TRANSITION__ECC:
			setECC((ECC) null);
			return;
		default:
			super.eUnset(featureID);
			return;
		}
	}

	/**
	 * <!-- begin-user-doc --> <!-- end-user-doc -->
	 * 
	 * @generated
	 */
	@Override
	public boolean eIsSet(int featureID) {
		switch (featureID) {
		case LibraryElementPackage.EC_TRANSITION__COMMENT:
			return COMMENT_EDEFAULT == null ? comment != null : !COMMENT_EDEFAULT.equals(comment);
		case LibraryElementPackage.EC_TRANSITION__CONDITION_EXPRESSION:
			return CONDITION_EXPRESSION_EDEFAULT == null ? conditionExpression != null
					: !CONDITION_EXPRESSION_EDEFAULT.equals(conditionExpression);
		case LibraryElementPackage.EC_TRANSITION__SOURCE:
			return source != null;
		case LibraryElementPackage.EC_TRANSITION__DESTINATION:
			return destination != null;
		case LibraryElementPackage.EC_TRANSITION__CONDITION_EVENT:
			return conditionEvent != null;
		case LibraryElementPackage.EC_TRANSITION__ECC:
			return basicGetECC() != null;
		default:
			return super.eIsSet(featureID);
		}
	}

	/**
	 * <!-- begin-user-doc --> <!-- end-user-doc -->
	 * 
	 * @generated
	 */
	@Override
	public String toString() {
		if (eIsProxy())
			return super.toString();

		StringBuilder result = new StringBuilder(super.toString());
		result.append(" (comment: "); //$NON-NLS-1$
		result.append(comment);
		result.append(", conditionExpression: "); //$NON-NLS-1$
		result.append(conditionExpression);
		result.append(')');
		return result.toString();
	}

} // ECTransitionImpl
